
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ImageManager{

    private Map<String, String> imageMap = new HashMap<>();

    public ImageManager() {
        FileUtils.ensureDirectoryExists("images");
    }

    public void downloadImage(String url) {
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        String savePath = "images/" + fileName;
        try {
            FileUtils.saveImage(url, savePath);
            imageMap.put(fileName, savePath);
            FileUtils.appendLog("Downloaded: " + url + " -> " + savePath);
            System.out.println("下载成功。");
        }catch (IOException e){

            System.out.println("下载失败"+e.getMessage());

        }
    }

    public void listImages() {
        if (imageMap.isEmpty()) {
            System.out.println("还没有下载任何图片");
            return;
        }

        System.out.println("已下载的图片列表:");
        Set<String> imageNames = imageMap.keySet();
        int index = 1;
        for (String name : imageNames) {
            System.out.println(index + ". " + name + " -> " + imageMap.get(name));
            index++;
        }
    }

    /**
     * 根据名称搜索图片
     * @param name 要搜索的图片名称
     */
    public void searchImage(String name) {
        boolean found = false;
        System.out.println("搜索结果:");

        for (Map.Entry<String, String> entry : imageMap.entrySet()) {
            if (entry.getKey().contains(name)) {
                System.out.println(entry.getKey() + " -> " + entry.getValue());
                found = true;
            }
        }

        if (!found) {
            System.out.println("没有找到包含 \"" + name + "\" 的图片");
        }
    }


}







